<?php

namespace app\common\model\xiservice;

use think\Model;
use think\Db;
use traits\model\SoftDelete;
use addons\xiservice\controller\WeixinMini;
use app\common\model\xiservice\Wxtemplate;
use app\common\model\xiservice\User as XiserviceUser;

class Message extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'xiservice_message';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'createtime_text'
    ];


    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\xiservice\User', 'user_id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }


    public function order()
    {
        return $this->belongsTo('app\common\model\xiservice\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function service()
    {
        return $this->belongsTo('app\common\model\xiservice\Service', 'service_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function sendOrderMessage($order, $status)
    {
        $data['type'] = 2;
        $data['order_id'] = $order['id'];
        $data['order_status'] = $status;
        $data['service_id'] = $order['service_id'];
        $data['user_id'] = $order['user_id'];
        $data['to_user_id'] = $order['worker_id'];
        $data['createtime'] = time();
        $data['updatetime'] = time();
        $message = '';

        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        if ($status == 1) {
            //用户下单
            $end_time = $order['service_start_time'] + $order['service_duration'] * 60;
            $data['title'] = '新订单通知';
            $data['content'] = '服务时间:' . date("Y-m-d H:i", $order['service_start_time']) . "-" . date("H:i", $end_time);

            if ($data['to_user_id']) {
                //微信推送
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'order_come');
            }

        } else if ($status == 2) {
            //用户取消订单
            $data['title'] = '订单取消通知';
            $data['content'] = date("H:i", time()) . ' 用户已取消订单';
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'order_cancel');
            }
        } else if ($status == 3) {
            //用户评价订单
            $data['title'] = '订单评价通知';
            $data['content'] = date("H:i", time()) . ' 用户已评价订单';
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'order_review');
            }
        } else if ($status == 4) {
            //服务人员已接单
            $data['title'] = '接单提醒';
            $data['content'] = date("H:i", time()) . ' 施工队已接单';
            $data['user_id'] = $order['worker_id'];
            $data['to_user_id'] = $order['user_id'];
            $userid = $order['user_id'];
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'order_confirm');
            }
        } else if ($status == 5) {
            $data['title'] = '服务提醒';
            $data['content'] = '您的订单还有30分钟开始,请提前做好准备';
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'order_start');
            }
        } else if ($status == 6) {
            //服务人员申请完工验收
            $data['title'] = '验收提醒';
            $data['content'] = date("H:i", time()) . ' 服务人员申请验收完工';
            $data['user_id'] = $order['worker_id'];
            $data['to_user_id'] = $order['user_id'];
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'check_apply');
            }
        } else if ($status == 7) {
            //雇主验收结果推送
            $data['title'] = '验收通过';
            $data['content'] = date("H:i", time()) . ' 雇主验收完工通过';
            if($order['check_status'] == '40') {
                $data['title'] = '验收不通过提醒';
                $data['content'] = date("H:i", time()) . ' 雇主验收完工不通过';
            }

            $data['user_id'] = $order['user_id'];
            $data['to_user_id'] = $order['worker_id'];
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'check_finish');
            }
        }else if ($status == 8) {
            //服务人员已接单
            $data['title'] = '用户选中提醒';
            $data['content'] = date("H:i", time()) . ' 施工队已接单';
            $data['user_id'] = $order['worker_id'];
            $data['to_user_id'] = $order['user_id'];
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('id', $data['user_id'])->value('mini_openid');
                $message = $temp->messageData($order, $openid, 'change_worker');
            }
        }else if ($status == 9) {
            //循环通知所有施工队
            $end_time = $order['service_start_time'] + $order['service_duration'] * 60;
            $data['title'] = '新订单通知';
            $data['content'] = '服务时间:' . date("Y-m-d H:i", $order['service_start_time']) . "-" . date("H:i", $end_time);
            $serviceuserList =  DB::name('xiservice_user')->where('group_id', 2)->select();
            foreach( $serviceuserList as $service)
            {
                $orderCity =  $order['citytxt'];
                $userCity =$service['citytxt'];  

                $service_category_id = $service['service_category_id'];
                $order_category_id =  strval($order['service_category_id']);
                $pos = strpos($service_category_id, $order_category_id);   
                if($orderCity==  $userCity && $pos !== false)
                {
                    $openid = $service['mini_openid'];
                    $messagetemp = $temp->messageData($order, $openid, 'order_come');
                    $result = $wx->subscribeMessage($messagetemp);
                }
               
            }
        }  else if ($status == 10) {
            //循环通知所有专家
            // $end_time = $order['service_start_time'] + $order['service_duration'] * 60;
            $data['title'] = '新订单通知';
            $data['content'] = '下单时间:' . date("Y-m-d H:i:s");
            $serviceuserList =  DB::name('xiservice_user')->where('group_id', 5)->select();
            foreach($serviceuserList as $service)
            {
                $orderCity =  $order['citytxt'];
                $userCity =$service['citytxt'];
                if($orderCity==  $userCity)
                {
                    $openid = $service['mini_openid'];
                    $messagetemp = $temp->messageData($order, $openid, 'order_come_zj');
                    $result = $wx->subscribeMessage($messagetemp);
                }
               
            }
        } else if ($status ==11) {
            $data['title'] = '工单完成';
            $data['content'] = date("H:i", time()) . ' 施工队已接单';
            $data['user_id'] = $order['worker_id'];
            $data['to_user_id'] = $order['user_id'];
            //微信推送
            if ($data['to_user_id']) {
                $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
                $order['param']='id='.$order['id'];
                $message = $temp->messageData($order, $openid, 'order_diagnose');
            }
        } 
        if ($message) {
//            dump($message);
            $result = $wx->subscribeMessage($message);
        }
        $res = $this->insertGetId($data);
    }

   
    public function sendRemindUserMessage($order)
    {

        $data['type'] = 2;
        $data['order_id'] = $order['id'];
        $data['order_status'] = 5;
        $data['service_id'] = $order['service_id'];
        $data['user_id'] = $order['worker_id'];
        $data['to_user_id'] = $order['user_id'];
        $data['createtime'] = time();
        $data['updatetime'] = time();
        $data['title'] = '服务提醒';
        $data['content'] = '您的订单还有30分钟开始,请提前做好准备';
        $res = $this->insertGetId($data);
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($data['to_user_id']) {
            $openid = XiserviceUser::where('user_id', $data['to_user_id'])->value('mini_openid');
            $message = $temp->messageData($order, $openid, 'order_start');
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
        $res = $this->insertGetId($data);

    }

    public function sendCheckUserMEssage($uid, $checkInfo){
        
   
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($uid) {
            $openid = XiserviceUser::where('user_id', $uid)->value('mini_openid');
            $message = $temp->messageData1($checkInfo, $openid, 'order_cancel');
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
    }
    //审批拒绝
    public function sendRefuseMEssage($uid, $checkInfo){        
   
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($uid) {
            $openid = XiserviceUser::where('user_id', $uid)->value('mini_openid');
            $reason = XiserviceUser::where('user_id', $uid)->value('reason');
            $checkInfo['reason']= $reason;
            $message = $temp->messageData_audit_refuse($checkInfo, $openid, 'audit_refuse');
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
    }

     //审批拒绝
     public function sendtixianMEssage($uid, $checkInfo){        
   
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($uid) {
            $openid = XiserviceUser::where('user_id', $uid)->value('mini_openid');
            $reason = XiserviceUser::where('user_id', $uid)->value('reason');
            $checkInfo['reason']= $reason;
            $message = $temp->messageData_tixian_refuse($checkInfo, $openid, 'audit_refuse');
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
    }

      //积分变动
      public function sendScoreMEssage($uid, $checkInfo,$cur_score){        
   
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($uid) {
            $openid = XiserviceUser::where('user_id', $uid)->value('mini_openid');
            $message = $temp->messageData_score($checkInfo, $openid, 'score_change',$cur_score);
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
    }
       //保证金变动
       public function sendMoneyMEssage($uid, $checkInfo,$cur_money){        
   
        $temp = new Wxtemplate();
        $wx = new WeixinMini();
        $message = '';
        //微信推送
        if ($uid) {
            $openid = XiserviceUser::where('user_id', $uid)->value('mini_openid');
            $message = $temp->messageData_money($checkInfo, $openid, 'score_change',$cur_money);
        }
        if ($message) {
            $result = $wx->subscribeMessage($message);
        }
    }

}
